package code1.double_pointer;

/**
 * 原题链接
 */
public class Offer05 {

    public String replaceSpace(String s) {
        int count=0;
        char[] oldArr = s.toCharArray();
        int oldLength=oldArr.length;
        for(int i=0;i<oldLength;i++){
            if(oldArr[i]==' '){
                count++;
            }
        }
        int newLength=oldArr.length+2*count;
        char[] newArr=new char[newLength];
        for(int i=0;i<oldLength;i++){
            newArr[i]=oldArr[i];
        }
        for(int i=oldLength-1,j=newLength-1;i<j;i--,j--){
            if(newArr[i]!=' '){
                newArr[j]=oldArr[i];
            }else{
                newArr[j--]='0';
                newArr[j--]='2';
                newArr[j]='%';
            }
        }
        StringBuilder sb=new StringBuilder();
        for(int i=0;i<newLength;i++){
            sb.append(newArr[i]);
        }
        return sb.toString();
    }
}
